<?php
class CourseAction extends StudyBaseAction
{
	/**
	 * 课程列表
	 */
	public function courseList()
	{
		$course = M('Course');
		if(isset($_POST['course_name']) && $_POST['course_name']!=null){
			$where['s_course.course_name'] = array('like',"%{$_POST['course_name']}%");
		}
		import('ORG.Util.Page');
		$count = $course->where($where)->count();// 查询满足要求的总记录数
		$page = new Page($count,10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $course->field('s_course.*,c.name')->where($where)->join("left JOIN s_center c on center_id = c.id")->limit($limit)->order("c.name desc")->select();
		$show = $page->show();
		$this->assign('page',$show);
		$this->assign('course_name',$_POST['course_name']);
		$this->assign('list',$list);
		$p = $_GET['p'];
		if(empty($p)){
			$p = 1;
		}
		$this->assign('p',$p);
		$this->display('Course:courseList');
	}
	/**
	 * 查看
	 */
	public function courseLook()
	{
		$form = M('Course');
		$where["s_course.id"] = $_GET['id'];
		$list = $form->field('s_course.*,c.name')->where($where)->join("left JOIN s_center c on center_id = c.id")->find();

		$game = M("Course_image");
		$course_image["course_id"] = $_GET['id'];
		$courseImage = $game->where($course_image)->select();

		$course_two = M("Two_course");
		$where_two["course_id"] = $_GET['id'];
		$twoCourse = $course_two->where($where_two)->select();

		$this->assign("list",$list);
		$this->assign("courseImage",$courseImage);
		$this->assign("twoCourse",$twoCourse);
		$this->display();
	}
	/**
	 * 编辑页面
	 */
	public function courseEditView()
	{
		$form = M('Course');
		$where["s_course.id"] = $_GET['id'];
		$list = $form->field('s_course.*,c.name')->where($where)->join("left JOIN s_center c on center_id = c.id")->find();

		$game = M("Course_image");
		$course_image["course_id"] = $_GET['id'];
		$courseImage = $game->where($course_image)->select();

		$course_two = M("Two_course");
		$where_two["course_id"] = $_GET['id'];
		$twoCourse = $course_two->where($where_two)->select();

		$this->assign("list",$list);
		$this->assign("courseImage",$courseImage);
		$this->assign("twoCourse",$twoCourse);
		$center = M('Center');
		$centerList = $center->select();
		$this->assign('centerList',$centerList);
		$center_type = M('Course_type');
		$centerTypeList = $center_type->select();
		$this->assign('centerTypeList',$centerTypeList);
		$this->assign('p',$_GET['p']);
		$this->display();
	}
	/**
	 * 设成封面
	 */
	public function courseEditFm()
	{
		$game = M("Course_image");
		$data['type'] = '0';
		$course_image["id"] = $_GET['id'];
		$course["course_id"] = $_GET['course_id'];
		$game->where($course)->data($data)->save();
		$data1['type'] = '1';
		$qq = $game->where($course_image)->data($data1)->save();
		$this->redirect('Course:courseEditView?id='.$_GET['course_id']);
	}
	/**
	 * 删除
	 */
	public function courseDel()
	{
		$course = M("Course"); // 实例化Center对象
		$course_image = M('Course_image');
		$two_course = M('Two_course');
		$id['id']=$_GET['id'];
		$course_id['course_id']=$_GET['id'];
		$course_image->where($course_id)->delete(); // 删除图片
		$two_course->where($course_id)->delete(); // 删除二级课程
		$course->where($id)->delete(); // 删除机构
		//$_SESSION['message'] = '删除成功！';
		$p = $_GET['p'];
		$this->redirect('Course:courseList?p='.$p);
	}
	/**
	 * 新增页面
	 */
	public function courseAddView()
	{
		$center = M('Center');
		$centerList = $center->select();
		$this->assign('centerList',$centerList);
		$center_type = M('Course_type');
		$centerTypeList = $center_type->select();
		$this->assign('centerTypeList',$centerTypeList);
		$this->display();
	}
	/**
	 * 新增
	 */
	public function courseAdd()
	{
		//新增课程
		$course = M('Course');
		$course->create();
		$curr_time = date('Y-m-d H:i',time());
		$course->create_time=$curr_time;
		$id = $course->add();
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/';// 设置附件上传目录
		//$upload->thumb=true;//是否开启图片文件缩略
		//$upload->thumbMaxWidth='400,100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		//$upload->thumbMaxHeight='400,100';
		//$upload->thumbPrefix='s_,m_';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		// 保存表单数据 包括附件数据
		$game = M("Course_image");
		$game->create();
		for($i=0;$i<count($info);$i++){
			if($i == 0){
				$game->type = '1';
			}else{
				$game->type = '0';
			}
			$game->image = $info[$i]['savename'];   //等同与$img=array('img'=>$data[0]['savename']);
			$game->course_id = $id;
			$game->add();
		}
		$twoCourse = M("Two_course");
		$two = $_POST['two_course'];
		for($i=0;$i<count($two);$i++){
			$twoCourse->two_course_name = $two[$i];
			$twoCourse->course_id = $id;
			$twoCourse->add();
		}
		$this->redirect('Course:courseList');
	}
	/**
	 * 提交修改
	 */
	public function courseEdit()
	{
		//修改机构
		$course = M('Course');
		$save_course['center_id'] = $_POST['center_id'];
		$save_course['course_name'] = $_POST['course_name'];
		$save_course['price'] = $_POST['price'];
		$save_course['sbprice'] = $_POST['sbprice'];
		$save_course['editor'] = $_POST['editor'];
		$save_course['type'] = $_POST['type'];
		$where_course['id'] = $_POST['id'];
		$course->where($where_course)->data($save_course)->save();
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/';// 设置附件上传目录
		//$upload->thumb=true;//是否开启图片文件缩略
		//$upload->thumbMaxWidth='400,100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		//$upload->thumbMaxHeight='400,100';
		//$upload->thumbPrefix='s_,m_';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		// 保存表单数据 包括附件数据
		$game = M("Course_image");
		//$game->create();
		for($i=0;$i<count($info);$i++){
			$game->image = $info[$i]['savename'];   //等同与$img=array('img'=>$data[0]['savename']);
			$game->course_id = $where_course['id'];
			$game->add();
		}
		$twoCourse = M("Two_course");
		$two = $_POST['two_course'];
		for($i=0;$i<count($two);$i++){
			$twoCourse->two_course_name = $two[$i];
			$twoCourse->course_id = $where_course['id'];
			$twoCourse->add();
		}
		$this->redirect('Course:courseList');
	}
	/**
	 * 编辑时删除图片
	 */
	public function delImg()
	{
		$game = M("Course_image");
		$where['id']=$_POST['id'];
		$game->where($where)->delete(); // 删除
		$this->ajaxReturn("","JSON");
	}
	/**
	 * 编辑时删除二级课程
	 */
	public function delTwo()
	{
		$game = M("Two_course");
		$where['id']=$_POST['id'];
		$game->where($where)->delete(); // 删除
		$this->ajaxReturn("","JSON");
	}
	/**
	 * 课程分类列表
	 */
	public function courseTypeList()
	{
		$course_type = M('Course_type');
		if(isset($_POST['course_type']) && $_POST['course_type']!=null){
			$where['course_type'] = array('like',"%{$_POST['course_type']}%");
		}
		import('ORG.Util.Page');
		$count = $course_type->where($where)->count();// 查询满足要求的总记录数
		$page = new Page($count,10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $course_type->where($where)->limit($limit)->order("order_id asc,id desc")->select();
		$show = $page->show();
		$this->assign('page',$show);
		$this->assign('course_type',$_POST['course_type']);
		$this->assign('list',$list);
		$p = $_GET['p'];
		if(empty($p)){
			$p = 1;
		}
		$this->assign('p',$p);
		$this->display('Course:courseTypeList');
	}
	/**
	 * 删除
	 */
	public function coursetypeDel()
	{
		$course_type = M("Course_type"); // 实例化对象
		$id['id']=$_GET['id'];
		$course_type->where($id)->delete(); // 删除
		//$_SESSION['message'] = '删除成功！';
		$p = $_GET['p'];
		$this->redirect('Course:courseTypeList?p='.$p);
	}
	/**
	 * 新增页面
	 */
	public function courseTypeAddView()
	{
		$this->display();
	}
	/**
	 * 新增
	 */
	public function courseTypeAdd()
	{
		//新增课程
		$course_type = M('Course_type');
		$course_type->create();
		//$curr_time = date('Y-m-d H:i',time());
		//$course_type->create_time=$curr_time;
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/courseType/';// 设置附件上传目录
		$upload->thumb=true;//是否开启图片文件缩略
		$upload->thumbMaxWidth='100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		$upload->thumbMaxHeight='100';
		$upload->thumbPrefix='';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$course_type->image=$info[0]['savename'];
		}
		$id = $course_type->add();
		$this->redirect('Course:courseTypeList');
	}
	/**
	 * 编辑分类页面
	 */
	public function courseTypeEditView()
	{
		$form = M('Course_type');
		$where["id"] = $_GET['id'];
		$list = $form->where($where)->find();

		$this->assign("list",$list);
		$this->assign('p',$_GET['p']);
		$this->display();
	}
	/**
	 * 提交修改
	 */
	public function courseTypeEdit()
	{
		//修改
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/courseType/';// 设置附件上传目录
		$upload->thumb=true;//是否开启图片文件缩略
		$upload->thumbMaxWidth='100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		$upload->thumbMaxHeight='100';
		$upload->thumbPrefix='';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$save_course['image'] = $info[0]['savename'];
		}
		// $this->ajaxReturn($info[0]['savename'],'JSON');
		 
		$course_type = M('Course_type');
		$save_course['course_type'] = $_POST['course_type'];
		$save_course['order_id'] = $_POST['order_id'];
		$where_course['id'] = $_POST['id'];
		$course_type->where($where_course)->data($save_course)->save();
		$this->redirect('Course:courseTypeList');
	}
	//-------------------------------------------------------------------------
	/**
	 * 课程二级分类列表
	 */
	public function courseTwoList()
	{
		$course_two = M('Course_two');
		if(isset($_POST['course_two']) && $_POST['course_two']!=null){
			$where['course_two'] = array('like',"%{$_POST['course_two']}%");
		}
		import('ORG.Util.Page');
		$count = $course_two->where($where)->count();// 查询满足要求的总记录数
		$page = new Page($count,10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $course_two->field('s_course_two.*,ct.course_type as course_type')->where($where)->join('left join s_course_type ct on ct.id = s_course_two.type')->limit($limit)->order("ct.order_id asc,s_course_two.id desc")->select();
		$show = $page->show();
		$this->assign('page',$show);
		$this->assign('course_two',$_POST['course_two']);
		$this->assign('list',$list);
		$p = $_GET['p'];
		if(empty($p)){
			$p = 1;
		}
		$this->assign('p',$p);
		$this->display('Course:courseTwoList');
	}
	/**
	 * 删除
	 */
	public function coursetwoDel()
	{
		$course_two = M("Course_two"); // 实例化对象
		$id['id']=$_GET['id'];
		$course_two->where($id)->delete(); // 删除
		//$_SESSION['message'] = '删除成功！';
		$p = $_GET['p'];
		$this->redirect('Course:courseTwoList?p='.$p);
	}
	/**
	 * 新增页面
	 */
	public function courseTwoAddView()
	{
		$center_type = M('Course_type');
		$centerTypeList = $center_type->select();
		$this->assign('centerTypeList',$centerTypeList);
		$this->display();
	}
	/**
	 * 新增
	 */
	public function courseTwoAdd()
	{
		//新增课程
		$course_two = M('Course_two');
		$course_two->create();
		//$curr_time = date('Y-m-d H:i',time());
		//$course_two->create_time=$curr_time;
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/courseTwo/';// 设置附件上传目录
		$upload->thumb=true;//是否开启图片文件缩略
		$upload->thumbMaxWidth='100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		$upload->thumbMaxHeight='100';
		$upload->thumbPrefix='';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$course_two->image=$info[0]['savename'];
		}
		$id = $course_two->add();
		$this->redirect('Course:courseTwoList');
	}
	/**
	 * 编辑分类页面
	 */
	public function courseTwoEditView()
	{
		$form = M('Course_two');
		$where["id"] = $_GET['id'];
		$list = $form->where($where)->find();
		$center_type = M('Course_type');
		$centerTypeList = $center_type->select();
		$this->assign('centerTypeList',$centerTypeList);
		$this->assign("list",$list);
		$this->assign('p',$_GET['p']);
		$this->display();
	}
	/**
	 * 提交修改
	 */
	public function courseTwoEdit()
	{
		//修改
		//导入图片上传类
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
		$upload->savePath =  './Public/Uploads/image/courseTwo/';// 设置附件上传目录
		$upload->thumb=true;//是否开启图片文件缩略
		$upload->thumbMaxWidth='100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		$upload->thumbMaxHeight='100';
		$upload->thumbPrefix='';  //缩略图文件前缀
		//$upload->thumbSuffix='-s,-m';//后缀
		//在数据库当中也存在一个文件名即可
		//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
		if(!$upload->upload()) {// 上传错误提示错误信息
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$save_course['image'] = $info[0]['savename'];
		}
		// $this->ajaxReturn($info[0]['savename'],'JSON');
		 
		$course_two = M('Course_two');
		$save_course['course_two'] = $_POST['course_two'];
		$save_course['type'] = $_POST['type'];
		$where_course['id'] = $_POST['id'];
		$course_two->where($where_course)->data($save_course)->save();
		$this->redirect('Course:courseTwoList');
	}
}
?>